﻿#pragma once
#include "Lumina2D.h"
#include <iostream>

extern const int GRAPH_WIEGHT = 640;
extern const int GRAPH_HEIGHT = 560;

class StartScene : public Scene 
{
public:
	StartScene(const std::string& name) :Scene(name)
	{

	}

	void OnCreate()
	{
		game_name = new IMAGE;
		loadimage(game_name, _T("img\\GameName.png"));

		int button_w = 300;
		int button_h = 90;
		game_start_button = new Button((GRAPH_WIEGHT-button_w)/2, 300, button_w, 90, "Start Game");
		game_start_button->setFontSize(50);
		game_start_button->setFillColor(RED);

		game_quit_button = new Button((GRAPH_WIEGHT - button_w) / 2, 450, button_w, 90, "Quit Game");
		game_quit_button->setFontSize(50);
		game_quit_button->setFillColor(RED);

		player = new Sprite;
		player->AddState("Idle", _T("resources\\player\\idle.png"), 5, 500, true);
		player->SetState("Idle",Vector2(0,300));

	}

	void OnDestroy()
	{
		if (game_name)
		{
			delete game_name;
		}

		if(game_start_button)
		      delete game_start_button;

		if (game_quit_button)
		{
			delete game_quit_button;
		}

		if (player)
		{
			delete player;
		}
	}

	void OnActivate() 
	{

	}

	void OnDeactivate()
	{

	}

	void Update(float deltaTime)
	{

		inputHandler.update();
		//更新开始按键
		if (inputHandler.isMouseButtonDown(MouseButton::Left))
		{
			int mouse_x = inputHandler.getMouseX();
			int mouse_y = inputHandler.getMouseY();

			if (can_begin && game_start_button->isClicked(mouse_x, mouse_y))
			{
				can_begin = false;
				m_sceneManager->SwitchToScene("game_scene");
			}

			if (game_quit_button->isClicked(mouse_x, mouse_y))
			{
				closegraph();
			}
		}

		if (inputHandler.isMouseButtonUp(MouseButton::Left))
		{
			can_begin = true;
		}


		player->Update(deltaTime);

	}

	void Render()
	{
		if (game_name)
		{
			int w = game_name->getwidth();
			putimage_alpha((GRAPH_WIEGHT-w)/2, 100, game_name);
		}
		else
		{
			std::cout << "GameName loaded fail" << std::endl;
		}

		if (game_start_button)
		{
			setbkmode(TRANSPARENT);
			game_start_button->draw();
		}

		if (game_quit_button)
		{
			game_quit_button->draw();
		}

		if (player)
		{
			player->Draw(Camera(0,0));
		}
	}
private:
	IMAGE* game_name=nullptr;

	InputHandler inputHandler;
	Button* game_start_button=nullptr;
	Button* game_quit_button = nullptr;
	Sprite* player=nullptr;

	bool can_begin = true;
};